//
//  CachetApproveViewController.m
//  云律司
//
//  Created by 韦淋 on 2017/9/6.
//  Copyright © 2017年 test. All rights reserved.
//

#import "CachetApproveViewController.h"

@interface CachetApproveViewController ()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic, strong) UITableView *tablewView;
@property (nonatomic, strong) UIButton *barAddButton;
@property (nonatomic, strong) NSMutableArray *dataSource;


@end

@implementation CachetApproveViewController
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.barTitleLable.text = @"公章申请记录";
    self.view.backgroundColor = [UIColor whiteColor];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [self initUI];
    [self getData];
}
#pragma mark - Button Target
- (void)action_AddButton:(UIButton *)sender{
    AddInfoViewController *addVC = [[AddInfoViewController alloc] init];
    [self.navigationController pushViewController:addVC animated:YES];
}
#pragma mark -- Method
- (void)initUI{
    [self.view addSubview:self.tablewView];
    [self.view addSubview:self.barAddButton];
}
- (void)getData{
    [NetworkingManager getUrl:[NSString stringWithFormat:@"%@create_id=%@",SEAL_URL,[InfoSingleTon personInfoSingleTon].infoDic[@"user"][@"id"]] dic:nil Succeed:^(id responseData) {
        NSLog(@"responseData = %@",responseData);
        
        if (self.dataSource.count == 0) {
            [MBProgressHUD showSuccess:@"暂无更多数据"];
        }
        [self.tablewView reloadData];
        [self.tablewView.mj_header endRefreshing];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络异常"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
- (void)loadNewData{
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.dataSource removeAllObjects];
        [self getData];
        [_tablewView.mj_header endRefreshing];
    });
    
    
}


#pragma mark - TypeButtonDelegate
//实现代理
-(void)didClickTypeButtonAction:(UIButton*)button withIndex:(NSInteger)index
{
    NSLog(@"现在点击的是%ld个",index);
    
    [self.dataSource removeAllObjects] ;
    [self.tablewView reloadData];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self getData];
    
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 160;
}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
//    if (_isRefresh) {
//        return 0.0000001;
//    }
//    return 65;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CachetApproveCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cachetApproveCell" forIndexPath:indexPath];
    //    cell.titleLable.text = self.dataSource[indexPath.row][@"ay"];
    //    cell.caseNumLable.text = [NSString stringWithFormat:@"案件号： %@",self.dataSource[indexPath.row][@"ah_number"]];
    //    cell.bailorLable.text = [NSString stringWithFormat:@"委托人： %@",self.dataSource[indexPath.row][@"wtr"]];
    //    cell.adversaryLable.text = [NSString stringWithFormat:@"对方当事人： %@",self.dataSource[indexPath.row][@"dfdsr"]];
    //
    //    NSString *key = self.dataSource[indexPath.row][@"case_state"];
    //    NSString *case_state = [self.case_State valueForKey:key];
    //    cell.caseStatusLable.text = [NSString stringWithFormat:@"案件状态： %@",case_state];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //    CaseDetailViewController *CDVC = [[CaseDetailViewController alloc] init];
    //    CDVC.caseDetailDic = self.dataSource[indexPath.row];
    //    [self.navigationController pushViewController:CDVC animated:YES];
}
#pragma mark - Getter
- (NSMutableArray *)dataSource{
    if (!_dataSource) {
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}
- (UITableView *)tablewView{
    if (!_tablewView) {
        _tablewView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
        _tablewView.backgroundColor = [UIColor clearColor];
        _tablewView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tablewView.delegate = self;
        _tablewView.dataSource = self;
        _tablewView.estimatedRowHeight = 180;
        [_tablewView registerClass:[MyCaseCell class] forCellReuseIdentifier:@"cachetApproveCell"];
        
        //下拉刷新
        
        NSMutableArray *images = [NSMutableArray array];//图片
        for (int i = 1; i < 48; i++) {//假设50张图片
            [images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%zd",i]]];
        }
        MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        //        header.frame = CGRectMake(0, 0, SCREEN_WIDTH, 50);
        //        [header setImages:images forState:MJRefreshStateRefreshing];// 设置正在刷新状态的动画图片
        
        // 设置普通状态的动画图片
        [header setImages:@[images[0]] forState:MJRefreshStateIdle];
        // 设置即将刷新状态的动画图片（一松开就会刷新的状态）
        [header setImages:@[images[0]] forState:MJRefreshStatePulling];
        // 设置正在刷新状态的动画图片
        [header setImages:images forState:MJRefreshStateRefreshing];
        _tablewView.mj_header = header;// 设置header
        header.lastUpdatedTimeLabel.hidden = YES;// 隐藏时间
        header.stateLabel.hidden = YES;// 隐藏状态
        
    }
    return _tablewView;
}
- (UIButton *)barAddButton{
    if (!_barAddButton) {
        _barAddButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _barAddButton.frame = CGRectMake(335, 30, 25, 25);
        [_barAddButton setBackgroundImage:[UIImage imageNamed:@"jiahao"] forState:UIControlStateNormal];
        [_barAddButton addTarget:self action:@selector(action_AddButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _barAddButton;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
